/**
 * 
 */
package com.gmail.hh.flickrj.sample.android.tasks;

import java.io.ByteArrayOutputStream;
import java.util.HashSet;
import java.util.Set;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.gmail.hh.flickrj.sample.android.FlickrHelper;
import com.gmail.hh.flickrj.sample.android.helper.Const;
import com.gmail.hh.flickrj.sample.android.helper.SettingsManager;
import com.googlecode.flickrjandroid.Flickr;
import com.googlecode.flickrjandroid.oauth.OAuth;
import com.googlecode.flickrjandroid.oauth.OAuthToken;
import com.googlecode.flickrjandroid.people.User;
import com.googlecode.flickrjandroid.photos.PhotoList;
import com.googlecode.flickrjandroid.uploader.UploadMetaData;
import com.googlecode.flickrjandroid.uploader.Uploader;

public class UploadPhotoTask extends AsyncTask<OAuth, Void, String> {

	/**
	 *  Load Photostream asynchron, invoke callback handler after loading
	 *  author: helmut
	 */
	private Bitmap bitmap;
	private Context mContext;
	private String mTitle;
	private String mDescription;
	private boolean mShowProgress;
	private ProgressDialog mProgressDialog;

	// each Class using this Loader  must implement this interface
	IPhotoUploadedListener mCallbackUploaded;
	public interface IPhotoUploadedListener {
		public void onPhotoUploaded(String string);
	}
	

	/**
	 * @param flickrjAndroidSampleActivity
	 */
	public UploadPhotoTask(IPhotoUploadedListener callbackLoaded, Bitmap bitmap, 
			String title, String description,
			boolean showProgress, Context context) {
		this.mCallbackUploaded = callbackLoaded;
		this.bitmap = bitmap;
		this.mTitle = title;
		this.mDescription = description;
		this.mShowProgress = showProgress;
		this.mContext = context;
	}
	

	@Override
	protected void onPreExecute() {
		super.onPreExecute();

		if (mShowProgress && mContext!=null) {
			mProgressDialog = ProgressDialog.show(mContext,
					"", "Upload image ..."); 
			mProgressDialog.setCanceledOnTouchOutside(true);
			mProgressDialog.setCancelable(true);
			mProgressDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dlg) {
					UploadPhotoTask.this.cancel(true);
				}
			});
		}
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 */
	@Override
	protected String doInBackground(OAuth... arg0) {
		OAuthToken token = arg0[0].getToken();
		Flickr f = FlickrHelper.getInstance().getFlickrAuthed(token.getOauthToken(), 
				token.getOauthTokenSecret());
		Set<String> extras = new HashSet<String>();
		extras.add("url_sq"); 
		User user = arg0[0].getUser();
		try {
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			int quality = SettingsManager.getInstance().getJpegCompression(); // quality from 0 to 100
			bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
			byte[] bitmapdata = stream.toByteArray();
			UploadMetaData uploadMetaData = new UploadMetaData();
			uploadMetaData.setTitle(mTitle);
			uploadMetaData.setDescription(mDescription);
			Uploader up = f.getUploader();
			return up.upload("KIT_upload", bitmapdata, uploadMetaData);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(String resultUpload) {
		if (mProgressDialog != null ) {
			mProgressDialog.dismiss();
		}
		if (this.mCallbackUploaded != null ) {
			this.mCallbackUploaded.onPhotoUploaded(resultUpload);
		}
	}
	
}